﻿using DAL.DataProvider;
using DAL.DataTransferObject;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Text;

namespace DAL.DataAccessObject
{
    public class GiamGiaKHDAO:DataAccess
    {
        public void GiamGiaKH_AddNew(GiamGiaKH ggkh)
        {
            SqlCommand cmd = new SqlCommand("INSERT INTO GiamGiaKhach VALUES(@IdSale,@StartDate,@EndDate,@Note,@IdCustomer,@SaleOff)", GetConnection());
            cmd.Parameters.Add(new SqlParameter("@IdSale", SqlDbType.Char)).Value = ggkh.maGiamGia;
            cmd.Parameters.Add(new SqlParameter("@StartDate", SqlDbType.Date)).Value = ggkh.ngayBatDau.Date;
            cmd.Parameters.Add(new SqlParameter("@EndDate", SqlDbType.Date)).Value = ggkh.ngayKetThuc.Date;
            cmd.Parameters.Add(new SqlParameter("@Note", SqlDbType.NVarChar)).Value = ggkh.ghiChu;
            cmd.Parameters.Add(new SqlParameter("@IdCustomer", SqlDbType.Char)).Value = ggkh.maKhachHang;
            cmd.Parameters.Add(new SqlParameter("@SaleOff", SqlDbType.Int)).Value = ggkh.chietKhau;
            cmd.ExecuteNonQuery();
        }
        public void GiamGiaKH_Update(GiamGiaKH ggkh)
        {
            SqlCommand cmd = new SqlCommand("UPDATE GiamGiaKhach SET NgayBatDau=@StartDate,NgayKetThuc=@EndDate,GhiChu=@Note,ChietKhau=@SaleOff WHERE MaGiamGia=@Idsale",GetConnection());
            cmd.Parameters.Add(new SqlParameter("@IdSale", SqlDbType.Char)).Value = ggkh.maGiamGia;
            cmd.Parameters.Add(new SqlParameter("@StartDate", SqlDbType.Date)).Value = ggkh.ngayBatDau.Date;
            cmd.Parameters.Add(new SqlParameter("@EndDate", SqlDbType.Date)).Value = ggkh.ngayKetThuc.Date;
            cmd.Parameters.Add(new SqlParameter("@Note", SqlDbType.NVarChar)).Value = ggkh.ghiChu;
            cmd.Parameters.Add(new SqlParameter("@SaleOff", SqlDbType.Int)).Value = ggkh.chietKhau;
            cmd.ExecuteNonQuery();
        }
        public void GiamGiaKH_Delete(GiamGiaKH ggkh)
        {
            SqlCommand cmd = new SqlCommand("DELETE FROM GiamGiaKhach WHERE MaGiamGia=@IdSale", GetConnection());
            cmd.Parameters.Add(new SqlParameter("@IdSale", SqlDbType.Char)).Value = ggkh.maGiamGia;
            cmd.ExecuteNonQuery();
        }
        public List<GiamGiaKH> GiamGiaKH_GetData()
        {
            List<GiamGiaKH> listGGKH = new List<GiamGiaKH>();
            SqlDataAdapter sda = new SqlDataAdapter("SELECT * FROM GiamGiaKhach", GetConnection());
            DataTable dt = new DataTable();
            sda.Fill(dt);
            for (int i = 0; i < dt.Rows.Count;i++ )
            {
                GiamGiaKH ggkh = new GiamGiaKH();
                ggkh.maGiamGia = dt.Rows[i]["MaGiamGiaKhach"].ToString();
                ggkh.maKhachHang = dt.Rows[i]["MaKhachHang"].ToString();
                ggkh.ngayBatDau = Convert.ToDateTime(dt.Rows[i]["NgayBatDau"]);
                ggkh.ngayKetThuc = Convert.ToDateTime(dt.Rows[i]["NgayKetThuc"]);
                ggkh.ghiChu = dt.Rows[i]["GhiChu"].ToString();
                ggkh.chietKhau = Convert.ToInt32(dt.Rows[i]["ChietKhau"]);
                listGGKH.Add(ggkh);
            }
                return listGGKH;
        }
        public List<GiamGiaKH> GiamGiaKH_SerachByName(string a)
        {
            List<GiamGiaKH> listGGKH = new List<GiamGiaKH>();
            SqlCommand cmd = new SqlCommand("SELECT * FROM GiamGiaKhach", GetConnection());
            SqlDataReader dr;
            dr = cmd.ExecuteReader();
            while(dr.Read())
            {
                if(a==dr["MaKhachHang"].ToString())
                {
                    GiamGiaKH ggkh = new GiamGiaKH();
                    ggkh.maGiamGia = dr["MaGiamGia"].ToString();
                    ggkh.ngayBatDau = Convert.ToDateTime(dr["NgayBatDau"]);
                    ggkh.ngayKetThuc = Convert.ToDateTime(dr["NgayKetThuc"]);
                    ggkh.maKhachHang = dr["MaKhachHang"].ToString();
                    ggkh.chietKhau = Convert.ToInt32(dr["ChietKhau"]);
                    ggkh.ghiChu = dr["GhiChu"].ToString();
                    listGGKH.Add(ggkh);
                }
            }
            return listGGKH;
        }
    }
}
